package top.zhdev.community.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;
import top.zhdev.community.entity.Comment;
import top.zhdev.community.mapper.CommentMapper;
import top.zhdev.community.service.CommentService;
import top.zhdev.community.service.DiscussPostService;
import top.zhdev.community.utils.CommunityConstant;
import top.zhdev.community.utils.SensitiveFilter;

import java.util.List;

/**
 * 评论处理业务类
 */
@Service
public class CommentServiceImpl implements CommentService, CommunityConstant {

    private CommentMapper commentMapper;
    private DiscussPostService discussPostService;
    private SensitiveFilter sensitiveFilter;

    @Autowired
    public void setCommentMapper(CommentMapper commentMapper){
        this.commentMapper = commentMapper;
    }

    @Autowired
    public void setDiscussPostService(DiscussPostService discussPostService){
        this.discussPostService = discussPostService;
    }

    @Autowired
    public void setSensitiveFilter(SensitiveFilter sensitiveFilter) {
        this.sensitiveFilter = sensitiveFilter;
    }

    /**
     * 根据实体类型获取评论内容
     * @param entityType
     * @param entityId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Comment> getCommentsByEntity(int entityType, Integer entityId, int offset, int limit) {
        return commentMapper.getCommentByEntity(entityType,entityId,offset,limit);
    }

    /**
     * 获取指定评论的回复数量
     * @param entityType
     * @param entityId
     * @return
     */
    @Override
    public int getCommentCount(int entityType, int entityId) {
        return commentMapper.getCountByEntity(entityType,entityId);
    }

    /**
     * 获取指定用户的评论数量
     * @param userId
     * @return
     */
    @Override
    public int getCommentCountByUserId(int userId) {
        return commentMapper.getCommentCountByUseId(userId);
    }

    /**
     * 获取指定用户的评论内容
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Comment> getCommentsByUserId(int userId, int offset, int limit) {
       return commentMapper.getCommentsByUserId(userId,offset,limit);
    }

    /**
     * 添加回帖信息
     * @param comment
     * @return
     */
    @Override
    public int addComment(Comment comment) {
        if(comment == null){
            throw new IllegalArgumentException("评论的内容不能为空");
        }
        // 添加评论 HtmlUtils.htmlEscape将内容转换为HTML格式，在一定程度能防止攻击，例如，内容中存在alert这样的内容
        String content = HtmlUtils.htmlEscape(comment.getContent());
        comment.setContent(sensitiveFilter.filter(content));
        // 对评论内容做敏感词过滤

        int rows = commentMapper.insertComment(comment);

        // 更新帖子评论数量
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            int count = commentMapper.getCountByEntity(comment.getEntityType(),comment.getEntityId());
            discussPostService.updateCommentCount(comment.getEntityId(),count);
        }
        return rows;
    }

    /**
     * 通过实体id 获取评论
     * @param id
     * @return
     */
    @Override
    public Comment getCommentsByEntityId(int id) {
        return commentMapper.selectCommentsByEntityId(id);
    }

    /**
     * 通过userId 查询个人评论数量
     * @param userId
     * @return
     */
    @Override
    public int getCommentsRowsByUSerId(int userId) {
        return commentMapper.selectCommentRowsByUserId(userId);
    }
}
